package com.alibaba.dubbo.consumer;

import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.service.EchoService;
import com.alibaba.dubbo.rpc.service.GenericService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * Spring Boot 应用启动类
 *
 * Created by bysocket on 16/4/26.
 */
// Spring Boot 应用的标识
@SpringBootApplication
public class BootConsumer {

    public static void main(String[] args) {


        ConfigurableApplicationContext run = SpringApplication.run(BootConsumer.class,args);

        Consumer consumer = run.getBean(Consumer.class);

        System.out.println("consumer");
        System.out.println(consumer.demoService.getPermissions(1000L));


        Object result = consumer.genService.$invoke("sayHello", new String[] { "java.lang.String" }, new Object[] { "World" });


        EchoService echoService = (EchoService) consumer.demoService; // 强制转型为EchoService

// 回声测试可用性
        Object status = echoService.$echo("OK");

        assert(status.equals("OK"));


// 本端是否为消费端，这里会返回true
        boolean isConsumerSide = RpcContext.getContext().isConsumerSide();
// 获取最后一次调用的提供方IP地址
        String serverIP = RpcContext.getContext().getRemoteHost();
// 获取当前服务配置信息，所有配置信息都将转换为URL的参数
        String application = RpcContext.getContext().getUrl().getParameter("application");
// 注意：每发起RPC调用，上下文状态会变化

        System.out.println("isConsumerSide " + isConsumerSide);
        System.out.println("serverIP " + serverIP);
        System.out.println("application " + application);

    }

}
